<!DOCTYPE html>
<!--
Copyright 2011 WebDriver committers
Copyright 2011 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html>
<head>
  <title>find_element_test.html</title>
  <script src="../../test_bootstrap.js"></script>
  <script src="../../../atoms/inject/deps.js"></script>
  <script type="text/javascript">
    goog.require('bot.inject.cache');
    goog.require('bot.locators');
    goog.require('bot.response');
    goog.require('goog.dom');
    goog.require('goog.testing.jsunit');
    goog.require('goog.userAgent');
    goog.require('webdriver.atoms.inject.frame');
    goog.require('webdriver.atoms.inject.locators');
  </script>

  <script type="text/javascript">
    var wrappedDocument;

    function setUp() {
      if (document[bot.inject.cache.CACHE_KEY_]) {
        document[bot.inject.cache.CACHE_KEY_] = undefined;
      }

      wrappedDocument = {
        'ELEMENT': bot.inject.cache.addElement(document.body)
      };
    }

    function testInjectFindElementById() {
      var e = webdriver.atoms.inject.locators.findElement(
          'id', 'x', wrappedDocument);
      assertEquals(
          document.getElementById('x'),
          getElementFromCache(checkResponse(e)));
    }

    function testInjectFindElementByCss() {
      if (!document['querySelector']) {
        // CSS locators are not supported.
        return;
      }

      var e = webdriver.atoms.inject.locators.findElement(
          'css', '#wrong', wrappedDocument);
      assertEquals(
          document.querySelector('#wrong'),
          getElementFromCache(checkResponse(e)));
    }

    function testInjectFindElementByClassName() {
      var e = webdriver.atoms.inject.locators.findElement(
          'className', 'dogs', wrappedDocument);
      var expected = bot.locators.findElements({className: 'dogs'})[0];
      assertEquals(expected, getElementFromCache(checkResponse(e)));
    }

    function testInjectFindElementByName() {
      var e = webdriver.atoms.inject.locators.findElement(
          'name', 'after', wrappedDocument);
      assertEquals(
          bot.locators.findElements({name: 'after'})[0],
          getElementFromCache(checkResponse(e)));
    }

      function testInjectFindElementByLinkText() {
      var e = webdriver.atoms.inject.locators.findElement(
          'linkText', 'this is a link', wrappedDocument);
      assertEquals(
          goog.dom.$('link'),
          getElementFromCache(checkResponse(e)));
    }

    function testInjectFindElementByPartialLinkText() {
      var e = webdriver.atoms.inject.locators.findElement(
          'partialLinkText', 'is', wrappedDocument);
      assertEquals(
          document.getElementById('link'),
          getElementFromCache(checkResponse(e)));
    }

    function testInjectFindElementByTagName() {
      var e = webdriver.atoms.inject.locators.findElement(
          'tagName', 'form', wrappedDocument);
      assertEquals(
          document.getElementsByTagName('form')[0],
          getElementFromCache(checkResponse(e)));
    }

    function testInjectFindElementByXpath() {
      if (goog.userAgent.IE) {
        return;
      }

      var e = webdriver.atoms.inject.locators.findElement(
          'xpath', './/*[@id="after"]', wrappedDocument);
      assertEquals(
          document.getElementById('after'),
          getElementFromCache(checkResponse(e)));
    }

    function testInjectFindElementsByTagName() {
      var e = webdriver.atoms.inject.locators.findElements('tagName', 'div');
      assertEquals(4, checkResponse(e).length);
    }

    function testInjectFindElementsByXPath() {
      if (goog.userAgent.IE) {
        return;
      }

      var e = webdriver.atoms.inject.locators.findElements('xpath', './/a');
      assertEquals(7, checkResponse(e).length);
    }

    function testLocatingAnElementInAnotherFrame() {
      var response = webdriver.atoms.inject.frame.findFrameByIndex(0);
      var frameWindow = checkResponse(response);

      response = webdriver.atoms.inject.locators.findElement(
          'id', 'buttonText', null, frameWindow);
      var found = checkResponse(response);

      var el = getElementFromCache(found, window.frames[0].document);
      assertEquals('strong', el.tagName.toLowerCase());
      assertEquals('buttonText', el.id);
    }

    function testLocatingAChildElementInAnotherFrame() {
      var response = webdriver.atoms.inject.frame.findFrameByIndex(0);
      var frameWindow = checkResponse(response);

      response = webdriver.atoms.inject.locators.findElement(
          'id', 'buttonForm', null, frameWindow);
      var root = checkResponse(response);

      response = webdriver.atoms.inject.locators.findElement(
          'tag name', 'strong', root, frameWindow);
      var found = checkResponse(response);

      var el = getElementFromCache(found, window.frames[0].document);
      assertEquals('strong', el.tagName.toLowerCase());
      assertEquals('buttonText', el.id);

      response = webdriver.atoms.inject.locators.findElement(
          'id', 'buttonText', null, frameWindow);
      var found = checkResponse(response);

      var el = getElementFromCache(found, window.frames[0].document);
      assertEquals('strong', el.tagName.toLowerCase());
      assertEquals('buttonText', el.id);
    }

    function testLocatingManyElementsInAnotherFrame() {
      var response = webdriver.atoms.inject.frame.findFrameByIndex(0);
      var frameWindow = checkResponse(response);

      response = webdriver.atoms.inject.locators.findElements(
          'tag name', 'form', null, frameWindow);
      var found = checkResponse(response);
      assertEquals(3, found.length);

      var doc = window.frames[0].document;
      assertEquals('form', getElementFromCache(found[0], doc).id);
      assertEquals('otherForm', getElementFromCache(found[1], doc).id);
      assertEquals('buttonForm', getElementFromCache(found[2], doc).id);
    }

    function checkResponse(strResponse) {
      var response = goog.json.parse(strResponse);
      bot.response.checkResponse(response);
      return response['value'];
    }

    function getElementFromCache(parsed, opt_doc) {
      return bot.inject.cache.getElement(parsed.ELEMENT, opt_doc);
    }
  </script>
</head>
<body>
  <p id="x" name="para">Para</p>

  <div name="after" id="wrong" class="feline cats">nope</div>
  <div name="right" id="after" class="dogs">yup</div>
  <div name="lion" class="cats">simba</div>
  <div name="tiger" class="cats">shere khan</div>

  <form action="#">
    <input name="after" /><br />
    <input name="foo" />
  </form>

  <!-- This comment should be ignored -->

  <span name="foo">Furrfu</span>

  <ul>
    <li id="illegal">item
    <li id="illegal">item
    <li id="illegal">item
    <li id="illegal">item
  </ul>

  <a id="link" href="#">this is a link</a>
  <a name="fishsticks">this is a link</a>
  <a href="#">this is a link</a>
  <a href="#">this is a link</a>
  <a href="#">this is a link</a>

  <a href="#">unrelated</a>
  <a href="#" id="empty-link"></a>

  <iframe id="frame" src="click_iframe.html"></iframe>
  </body>
</html>
